<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn Blog:http://blog.sina.com.cn/saverneo     |
// +------------------------------------------------------------------------+
//
/**
 * 拍单管理
 */
namespace AuctionBack\Controller;

use AuctionCore\CarDealer\Dealer;
use AuctionCore\System\AuctionException;
use AuctionCore\Account\Back\Employee;

class CouponController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    public function index()
    {
        //检查权限
        $e = new Employee($this->user_info['username']);
        $add_auth = $e->check_auth("XinJianDaiJinQuan");
        $give_auth = $e->check_auth("FaFangDaiJinQuan");

        $coupon = new \AuctionCore\Other\BaseCoupon();
        $coupon_type = $coupon->get_coupon_type(false,$this->user_info['branch_id']);
        

        //获取分公司
        $branch_company  = $this->get_branch_company_list();

        $this->assign("branch_company", $branch_company);
        $this->assign("coupon_type", $coupon_type);
        $this->assign('add_auth', $add_auth);
        $this->assign('give_auth', $give_auth);
        $this->display();
    }

    //代金券列表
    public function coupon_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['type'] = I("get.type");
        $params['status'] = I("get.status");
        $params['use_status'] = I("get.use_status");
        $params['keyword'] = urldecode(I("get.keyword"));
        $params['branch_id'] = I("get.branch_id");
        $params['grant_type'] = I('get.grant_type');

        if(empty($params['branch_id'])){
            $params['branch_id'] = $this->user_info['branch_id'];
        }

        //获取list
        $ol = new \AuctionCore\Other\CouponList();
        $result = $ol->getList($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 发放代金券(列表)
     * @return \Think\assign|void
     */
    public function assign_coupon()
    {
        $coupon = new \AuctionCore\Other\BaseCoupon();
        $coupon_type = $coupon->get_coupon_type(true,$this->user_info['branch_id']);
        $this->assign("coupon_type", $coupon_type);
        $this->display('assign');
    }

    /**
     * 发放代金券(动作)
     * @return \Think\assign|void
     */

    public function do_assign_coupon()
    {
        $dealer_id = I('post.dealer_id');
        $coupon_type_id = I('post.coupon_type_id');
        $num = I('post.num');

        $code = '200';
        $msg = 'SUCCESS';

        if (empty($dealer_id)) {
            $code = '540';
            $msg = '代金券发放的车商ID不能为空';
        }
        $d = new Dealer($dealer_id);
        $dealer_info = $d->detail();
        if(empty($dealer_info)){
           $this->helpJsonResult(400, "车商不存在");
        }
        if($dealer_info['category']!=1){
            $this->helpJsonResult(400, "车商不是正式账号");
        }
        if (empty($coupon_type_id)) {
            $code = '539';
            $msg = '代金券的类型不能为空';
        }

        if (empty($num)) {
            $code = '538';
            $msg = '代金券发放的数量不能为空';
        }
        $coupon = new \AuctionCore\Other\BaseCoupon();

        //
        $data = array();
        $data['dealer_id'] = $dealer_id;
        $data['num'] = $num;
        $data['coupon_type_id'] = $coupon_type_id;
        //余量检查
        $count = $coupon->get_coupons(array('c.coupon_type_id' => array('eq', $data['coupon_type_id'])), true);
        if ($count < $num) {
            $code = '541';
            $msg = '该类型面额的代金券数量不足';
        }
        try {
            $res = $coupon->assign_coupon($data);
            if ($res) {
                $code = '200';
                $msg = '代金券发放成功';

            } else {
                $msg = '代金券发放失败';
            }
        } catch (AuctionException $e) {
            $code = $e->getCode();
            $msg = $e->getMessage();
        }
        $this->helpJsonResult($code, $msg);
    }

    //车商列表
    public function assign_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['type'] = I("get.s_type");
        $params['status'] = I("get.s_status");
        $params['amount'] = I("get.s_amount");
        $params['use_status'] = I("get.use_status");
        $params['keyword'] = urldecode(I("get.keyword"));
        $params['depart_id'] = $this->user_info['branch_id'];
        //获取list
        $cd = new \AuctionCore\CarDealer\DealerList();
        $result = $cd->getDealers($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /*
     * 车商代金券详情
     * */
    public function detail()
    {
        $dealer_id = I('get.dealer_id');
        $dealer = new \AuctionCore\CarDealer\Dealer($dealer_id);
        $detail = $dealer->detail();
        $coupon = new \AuctionCore\Other\BaseCoupon();
        //代金卷类型下拉菜单
        $coupon_type = $coupon->get_coupon_type(false,$this->user_info['branch_id']);
        //代金券统计
        $instance = new \AuctionCore\Other\Coupon($dealer_id);
        $stat = $instance->coupon_stat();
        $this->assign("stat", $stat);
        $this->assign('coupon_type', $coupon_type);
        $this->assign('detail', $detail);
        $this->display('detail');
    }

    /*
     * 车商代金券列表
     * */
    public function detail_list()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['dealer_id'] = I("get.dealer_id");
        $params['curr_tab'] = I("get.curr_tab");
        $params['coupon_type_id'] = I("get.coupon_type_id");
        $params['get_time'] = I("get.get_time");
        $params['end_date'] = I("get.end_date");
        //获取list
        $cd = new \AuctionCore\CarDealer\DealerList();
        $result = $cd->getDealerCoupons($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /*
     *新增代金券
     * */
    public function add()
    {
        $code = '200';
        $msg = 'SUCCESS';
        $type = I('post.type');
        $amount = I('post.amount');
        $num = I('post.num');
        $start_date = I('post.start_date');
        $end_date = I('post.end_date');
        $branch_id = I('post.branch_id');

        if (empty($type)) {
            $code = '532';
            $msg = '代金券类型不能为空';
        }
        if (empty($amount)) {
            $code = '533';
            $msg = '代金券面值要大于零';
        }
        if (empty($num)) {
            $code = '531';
            $msg = '代金券生成数量要大于零';
        }
        if (empty($start_date)) {
            $code = '534';
            $msg = '代金券开始日期不能为空';
        }
        if (empty($end_date)) {
            $code = '535';
            $msg = '代金券结束日期不能为空';
        }

        if (empty($branch_id)) {
            $code = '535';
            $msg = '所属公司不能为空';
        }

        if ($code == '200') {
            $data = array();
            $data['emp_id'] = $this->emp_id;
            $data['type'] = $type;//类型，名称，标题
            $data['amount'] = $amount;
            $data['num'] = $num;
            $data['start_date'] = $start_date;
            $data['end_date'] = $end_date;
            $data['branch_company'] = $branch_id;
            $res = false;
            try {
                $coupon = new \AuctionCore\Other\BaseCoupon();
                $res = $coupon->gen($data);
            } catch (AuctionException $e) {
                $code = $e->getCode();
                $msg = $e->getMessage();

            }
            if ($res) {
                $msg = '代金券创建成功';
            } else {
                $code = '536';
                $msg = '代金券创建失败' . $msg;
            }
        }
        $this->helpJsonResult($code, $msg);
        exit;
    }

    //获取指定发放类型的代金券活动类型
    public function get_grant_coupon_type(){
        $grant_type = I('post.grant_type');
        $coupon = new \AuctionCore\Other\BaseCoupon();
        $coupon_type = $coupon->get_grant_coupon_type($grant_type,$this->user_info['branch_id']);
        if ($coupon_type) {
            foreach ($coupon_type as $key => $value) {
                if (!$value['a_id']) {
                    $coupon_type[$key]['type_name'] = $value['type_name']."(".$value['amount'].")";
                }
            }
        }
        echo json_encode($coupon_type);
    }


}